#!/bin/bash

Upgrade_Nginx()
{
    Cur_Nginx_Version=`/usr/local/nginx/sbin/nginx -v 2>&1 | cut -c22-`

    if [ -s /usr/local/include/jemalloc/jemalloc.h ] && /usr/local/nginx/sbin/nginx -V 2>&1|grep -Eqi 'ljemalloc'; then
        NginxMAOpt="--with-ld-opt='-ljemalloc'"
     elif [ -s /usr/local/include/gperftools/tcmalloc.h ] && grep -Eqi "google_perftools_profiles" /usr/local/nginx/conf/nginx.conf; then
        NginxMAOpt='--with-google_perftools_module'
     else
        NginxMAOpt=""
    fi
    if ! grep -Eqi "^#memc" /usr/local/nginx/conf/nginx.conf &&  /usr/local/nginx/sbin/nginx -V 2>&1 | grep -Eqi 'memc-nginx' ; then
        echo "Your Nginx with memc-nginx-module"
        cd ${cur_dir}/src
        Download_Files ${Download_Mirror}/web/nginx/srcache-nginx-module-master.zip srcache-nginx-module-master.zip
        Download_Files ${Download_Mirror}/web/nginx/memc-nginx-module-master.zip memc-nginx-module-master.zip
        unzip -o memc-nginx-module-master.zip
        unzip -o srcache-nginx-module-master.zip
        Nginx_Modules_Options="${Nginx_Modules_Options} --add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master"
        [[ ! -s /usr/local/memcached/bin/memcached ]] && Install_Memcached 2
     else
        echo "Your Nginx without memc-nginx-module"
    fi

    Nginx_Version=""
    echo "Current Nginx Version:${Cur_Nginx_Version}"
    echo "You can get version number from http://nginx.org/en/download.html"
    read -p "Please enter nginx version you want, (example: 1.7.8 ): " Nginx_Version
    if [ "${Nginx_Version}" = "" ]; then
        echo "Error: You must enter a nginx version!!"
        exit 1
    fi
    echo "+---------------------------------------------------------+"
    echo "|    You will upgrade nginx version to ${Nginx_Version}"
    echo "+---------------------------------------------------------+"

    Press_Start

    echo "============================check files=================================="
    cd ${cur_dir}/src
    if [ -s nginx-${Nginx_Version}.tar.gz ]; then
        echo "nginx-${Nginx_Version}.tar.gz [found]"
     else
        echo "Error: nginx-${Nginx_Version}.tar.gz not found!!!download now......"
        wget -c ${Download_Mirror}/nginx/nginx-${Nginx_Version}.tar.gz
        if [ $? -eq 0 ]; then
            echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
         else
            wget -c http://nginx.org/download/nginx-${Nginx_Version}.tar.gz
            if [ $? -eq 0 ]; then
                echo "Download nginx-${Nginx_Version}.tar.gz successfully!"
             else
                echo "You enter Nginx Version was:"${Nginx_Version}
                Echo_Red "Error! You entered a wrong version number, please check!"
                sleep 5
                exit 1
            fi
        fi
    fi
    echo "============================check files=================================="

    Tar_Cd nginx-${Nginx_Version}.tar.gz nginx-${Nginx_Version}
    if echo ${Nginx_Version} | grep -Eqi '^[0-1].[5-8].[0-9]' || echo ${Nnginx_Version} | grep -Eqi '^1.9.[1-4]'; then
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
     else
        ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module ${NginxMAOpt} ${Nginx_Modules_Options}
    fi
    make
    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-${Cur_Nginx_Version}.${Upgrade_Date}
    \cp objs/nginx /usr/local/nginx/sbin/nginx
    echo "Test nginx configure file..."
    /usr/local/nginx/sbin/nginx -t
    echo "upgrade..."
    make upgrade
    Echo_Green "======== upgrade nginx completed ======"
    echo "Program will display Nginx Version......"
    /usr/local/nginx/sbin/nginx -v
}